﻿using System;
using System.Linq;
using Bannerlord.UIExtenderEx;
using HarmonyLib;
using MarriageFertility.Behaviors;
using MarriageFertility.Helpers;
using MarriageFertility.Mcm;
using MarriageFertility.Models;
using MarriageFertility.Texts;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.ComponentInterfaces;
using TaleWorlds.Core;
using TaleWorlds.MountAndBlade;

namespace MarriageFertility
{
    public class SubModule : MBSubModuleBase
    {            
        private readonly UIExtender _extender = new ("MarriageFertility");
        protected override void OnSubModuleLoad()
        {
            base.OnSubModuleLoad();
            var harmony = new Harmony("jiangHe.MarriageFertility");
            harmony.PatchAll();
            _extender.Register(typeof (SubModule).Assembly);
            _extender.Enable();
        }
        
        protected override void OnGameStart(Game game, IGameStarter gameStarterObject)
        {
            base.OnGameStart(game, gameStarterObject);
            if (!(game.GameType is Campaign))
                return;
            AddModels(gameStarterObject as CampaignGameStarter);
            AddBehaviors(gameStarterObject as CampaignGameStarter);
        }
    
        private void AddModels(IGameStarter gameStarterObject)
        { 
            AddModel<MfMarriageModel, MarriageModel>(gameStarterObject);
            AddModel<MfPregnancyModel, PregnancyModel>(gameStarterObject);
            AddModel<MfPartySizeLimitModel, PartySizeLimitModel>(gameStarterObject);
            AddModel<MfAgeModel, AgeModel>(gameStarterObject);
        }

        private void AddBehaviors(CampaignGameStarter gameStarterObject)
        {
            if (gameStarterObject != null)
            {
                gameStarterObject.AddBehavior(new FamilyAndRelationsBehavior());
                gameStarterObject.AddBehavior(new CustomerPregnancyCampaignBehavior());
                gameStarterObject.AddBehavior(new SexRelationBehavior());
                gameStarterObject.AddBehavior(new NtrBehavior());
                gameStarterObject.AddBehavior(new DuelMenuBehavior());
                gameStarterObject.AddBehavior(new ClanLeaderFixBehavior()); 
                gameStarterObject.AddBehavior(new SpouseTournamentBehavior());
                gameStarterObject.AddBehavior(new ClinicBehavior());
                gameStarterObject.AddBehavior(new PartyLeaveRequestBehavior());
                gameStarterObject.AddBehavior(new LordEquipmentOnEnterTownBehavior());
                gameStarterObject.AddBehavior(new TavernSpouseBehavior());
            }
        }

        protected override void OnBeforeInitialModuleScreenSetAsRoot()
        {
            MCMSettings.Instance.Settings();
            CheckHotButter();
            Log.CommonMessage(CommonText.ModInfo, TextColor.Coral);
        }
        
        private static void CheckHotButter()
        {
            Type pompaType = AccessTools.TypeByName("PompaSceneNotificationItem");
            if(pompaType != null)
            {
                RelationHelper.HotButterFound = true;
            }
        }
        
        private void AddModel<TBloodlustModel, TBaseModel>(IGameStarter gameStarterObject) 
            where TBloodlustModel : GameModel 
            where TBaseModel : GameModel
        {
            var baseModel = GetGameModel<TBaseModel>(gameStarterObject);
            if (baseModel != null)
            {
                gameStarterObject.AddModel(Activator.CreateInstance(typeof(TBloodlustModel), baseModel) as TBloodlustModel);
            }
        }
        
        private T GetGameModel<T>(IGameStarter gameStarterObject) where T : GameModel
        {
            GameModel[] array = gameStarterObject.Models.ToArray();
            for (int index = array.Length - 1; index >= 0; index--)
            {
                T gameModel = array[index] as T;
                bool flag = gameModel != null;
                if (flag)
                {
                    return gameModel;
                }
            }
            return default;
        }
    }
}